﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.cms.businesslogic.web;

namespace SS4U.Section.Configuration
{
    public partial class CreateDocumentType : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sbmt.Text = ui.Text("create");
            if (!IsPostBack)
            {
                masterType.Attributes.Add("style", "width: 350px;");
                masterType.Items.Add(new ListItem(ui.Text("none") + "...", "0"));
                foreach (DocumentType dt in DocumentType.GetAllAsList())
                    masterType.Items.Add(new ListItem(dt.Text, dt.Id.ToString()));
            }
        }

        protected void sbmt_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                const int createTemplateVal = 0;
                const int nodeId = -1;

                string returnUrl = umbraco.presentation.create.dialogHandler_temp.Create(
                    helper.Request("nodeType"),
                    int.Parse(masterType.SelectedValue),
                    createTemplateVal,
                    rename.Text);

                if (returnUrl == "")
                {
                    //top.refreshTree(true, true);
                    if (nodeId > 0)
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "refresh", "<script>\ntop.reloadCurrentNode();\ntop.closeModal();</script>");
                    else
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "refresh", "<script>\ntop.reloadCurrentNode();\ntop.closeModal();</script>");
                }
                else
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "refresh", "<script type=\"text/javascript\">\ntop.reloadCurrentNode();\ntop.right.location.href = '" + returnUrl + "';\ntop.closeModal();</script>");
            }

        }
    }
}